//
//  SblbV.swift
//  Auscultation
//
//  Created by 来 on 2020/12/10.
//

import UIKit
import CoreBluetooth

class SblbV: UIView {
    
    var back_view: UIView!
    var collectionView: UICollectionView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.black.withAlphaComponent(0.2)
        
        back_view = UIView.init()
        back_view.layer.cornerRadius = 8
        back_view.backgroundColor = UIColor.white
        addSubview(back_view)
        back_view.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.7)
            make.height.equalToSuperview().multipliedBy(0.5)
        }
        
        let label1 = UILabel.init()
        label1.text = "请选择连接的设备"
        label1.textAlignment = .center
        label1.font = UIFont.boldSystemFont(ofSize: 18)
        label1.textColor = UIColor.init(lgf_HexString: "222222")
        back_view.addSubview(label1)
        label1.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(50)
        }
        
        let line1 = UIView.init()
        line1.backgroundColor = UIColor.init(lgf_HexString: "F1F2F4")
        back_view.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.top.equalTo(label1.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 样本列表
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout.lgf_Layout(0, 0, 0, 0, 0, 0))
        collectionView.backgroundColor = UIColor.init(lgf_HexString: "FFFFFF")
        collectionView.lgf_Register(cellType: SblbCell.self)
        collectionView.delegate = self
        collectionView.dataSource = self
        back_view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(line1.snp.bottom)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.bottom.equalToSuperview().offset(-50)
        }
        
        let line2 = UIView.init()
        line2.backgroundColor = UIColor.init(lgf_HexString: "F1F2F4")
        back_view.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.top.equalTo(collectionView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let label2 = UILabel.init()
        label2.text = "取消"
        label2.textAlignment = .center
        label2.font = UIFont.boldSystemFont(ofSize: 18)
        label2.textColor = UIColor.init(lgf_HexString: "222222")
        label2.lgf_AddTap(target: self, action: #selector(SblbV.quxiao(_:)))
        back_view.addSubview(label2)
        label2.snp.makeConstraints { (make) in
            make.top.equalTo(line2.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
    }
    
    func show(_ sv: UIView) {
        self.frame = sv.bounds
        sv.addSubview(self)
        self.collectionView.reloadData()
        back_view.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
        self.backgroundColor = UIColor.black.withAlphaComponent(0)
        UIView.animate(withDuration: 0.2) {
            self.back_view.transform = CGAffineTransform.identity
            self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
        } completion: { (finish) in
            EWEBluetooth.lgf.startScan()
            NotificationCenter.default.addObserver(self, selector: #selector(self.getPeripheral), name: NSNotification.Name(rawValue: "GET_PERIPHERAL"), object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(self.finishScan), name: NSNotification.Name(rawValue: "FINISH_SCAN"), object: nil)
        }
    }
    
    @objc func getPeripheral() {
        self.collectionView.reloadData()
    }
    
    @objc func finishScan() {
        self.collectionView.reloadData()
    }
    
    // 取消
    @objc func quxiao(_ sender: UITapGestureRecognizer) {
        EWEBluetooth.lgf.stopScan()
        UIView.animate(withDuration: 0.2) {
            self.back_view.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
            self.back_view.alpha = 0.0
            self.backgroundColor = UIColor.black.withAlphaComponent(0)
        } completion: { (finish) in
            self.removeFromSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension SblbV : UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  {
        return CGSize.init(width: collectionView.lgf_Width, height: 60)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return EWEBluetooth.lgf.peripherals.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.lgf_DequeueReusableCell(with: SblbCell.self, for: indexPath)
        let peripheral = EWEBluetooth.lgf.peripherals[indexPath.item]
        cell.name.text = peripheral.name ?? "未知"
        cell.uuid.text = peripheral.identifier.uuidString
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if EWEBluetooth.lgf.peripherals.count == 0 {
            return
        }
        let peripheral = EWEBluetooth.lgf.peripherals[indexPath.item]
        EWEBluetooth.lgf.connection(peripheral)
        self.removeFromSuperview()
    }
}
